package cn.efunbox.afw.data.helper;

import cn.efunbox.afw.core.enums.BaseOrderEnum;
import org.springframework.data.domain.Sort;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * SortHelper
 * Created by xusq on 2017/5/22.
 */
public class SortHelper {


    public static Sort sortMap2Sort(LinkedHashMap<String,BaseOrderEnum> sortMap){
        if (CollectionUtils.isEmpty(sortMap)) {
            return null;
        }

        List<Sort.Order> sorts = new ArrayList<>();
        sortMap.forEach((prop, order) -> {
            if (BaseOrderEnum.ASC.equals(order)) {
                sorts.add(new Sort.Order(Sort.Direction.ASC, prop));
            } else {
                sorts.add(new Sort.Order(Sort.Direction.DESC, prop));
            }
        });

        return new Sort(sorts);

    }
}
